program test_04_lights;

{*
Test lights (esp. diffuse light)
}

uses SysUtils, gl, glu, glut, opengls;

procedure OnDisplay; cdecl;
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glLoadIdentity;

  // face 1
  glColor3f(1,0,0);
  glBegin(GL_QUADS);
  glNormal3i(0,0,1);  glVertex3i(-1, 1, -10);
  glNormal3i(0,0,1);  glVertex3i(-1, -1, -10);
  glNormal3i(0,0,1);  glVertex3i(1, -1, -10);
  glNormal3i(0,0,1);  glVertex3i(1, 1, -10);
  glEnd;
  // face 2
  glColor3f(1,0.5,0.3);
  glBegin(GL_QUADS);
  glNormal3i(1,0,0);  glVertex3i(1, 1, -10);
  glNormal3i(1,0,0);  glVertex3i(1, -1, -10);
  glNormal3i(1,0,0);  glVertex3i(2, -1, -15);
  glNormal3i(1,0,0);  glVertex3i(2, 1, -15);
  glEnd;

  // refresh
  glutSwapBuffers;
  Sleep(50);
end;

procedure OnIdle; cdecl;
begin
  glutPostRedisplay;
end;

var Ambient: array[0..3] of glfloat = (0.5, 0.0, 0.0, 1.0);
    Diffuse: array[0..3] of glfloat = (0.0, 0.5, 0.0, 1.0);

begin
  glutInit(@argc,argv);
  glutInitDisplayMode(GLUT_RGBA or GLUT_DOUBLE or GLUT_DEPTH);
  glutInitWindowSize(640, 480);
  glutInitWindowPosition(
    (glutGet(GLUT_SCREEN_WIDTH) - 640) div 2,
    (glutGet(GLUT_SCREEN_HEIGHT) - 480) div 2);
  glutCreateWindow('Test04');
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glShadeModel(GL_SMOOTH);
  glClearDepth(1);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  glDepthFunc(GL_LESS);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity;
  gluPerspective(45, 1.3, 1, 5000);
  glMatrixMode(GL_MODELVIEW);
  glutDisplayFunc(@OnDisplay);
  glutIdleFunc(@OnIdle);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  glEnable(GL_CULL_FACE); 
  // lights
  glLightfv(GL_LIGHT0, GL_AMBIENT, @Ambient);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, @Diffuse);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  // main
  glutMainLoop;
end.
